﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using SpeedTestReports.XAMLReports;
using System.Windows.Markup;

namespace SpeedTestReports
{
    public partial class DashboardForm : ChildWindow
    {
        DispatcherTimer refreshTimer = null;

        public string UserName { get; set; }
        public string Password { get; set; }

        public DashboardForm()
        {
            InitializeComponent();
        }

        private void FormDashboard_Loaded(object sender, RoutedEventArgs e)
        {
            refreshTimer = new DispatcherTimer();
            refreshTimer.Tick += refreshTimer_Tick;
            refreshTimer.Interval = new TimeSpan(0, 2, 0); // 5 Minutes
            refreshTimer.Start();

            this.dashboardViewer.Height = this.ActualHeight;
            this.dashboardViewer.Width = this.ActualWidth;

            GetDashboardReport();   //get the initial load
        }

        private void FormDashboard_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Application.Current.Host.Content.IsFullScreen = false;
            if (refreshTimer != null) { refreshTimer.Stop(); }
        }

        void refreshTimer_Tick(object sender, EventArgs e)
        {
            GetDashboardReport();
        }

        private void GetDashboardReport()
        {
            XAMLReportsSoapClient client = new XAMLReportsSoapClient();
            client.GetDashboardReportCompleted += new EventHandler<GetDashboardReportCompletedEventArgs>(client_GetDashboardReportCompleted);
            client.GetDashboardReportAsync(UserName, Password);
        }

        void client_GetDashboardReportCompleted(object sender, GetDashboardReportCompletedEventArgs e)
        {
            string reportXaml = e.Result.ToString();
            this.dashboardViewer.Content = "";
            this.dashboardViewer.Content = ((UIElement)XamlReader.Load(reportXaml));    //example only

            //load the report in the viewer.
        }
    }
}

